const app = getApp();

Page({
  data: {
    hidden: false, //加载
    outline: false, //有无网络
    noData: false,
    default: 0,
    userId: '',
    token: '',
    list: [],
  },
  onLoad(query) {
    // 页面加载


  },
  onReady() {
    // 页面加载完成
  },
  // 添加新地址
  add: function() {
    my.navigateTo({
      url: '/pages/add/add',
    })
  },
  // 修改地址
  edit: function(e) {
    my.navigateTo({
      url: '/pages/edit/edit?id=' + e.currentTarget.dataset.id,
    })
  },
  // 删除地址
  del: function(e) {
    var that = this;
    var req = {
      id: e.currentTarget.dataset.id
    }
    my.request({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      url: 'https://www.tiantong369.com/crm/usershopaddressdel', // 该url是自己的服务地址
      method: 'POST',
      headers: {
        // 'x-access-token': that.data.token
      },
      data: req,
      dataType: 'json',
      success: (res) => {//删除成功
        my.showToast({
                      content: res.data.message
        });
         that.post()
      },
      fail: (res) => {
        console.log('GetEmpPrice:', res)
      },
    });

  },
  onShow() {
    var that = this;
    my.getNetworkType({
      success: (res) => {
        if (res.networkAvailable) {
          that.setData({
            outline: false,

          })
          // 移除保存的addressid
          my.removeStorage({
            key: 'addressId', // 缓存数据的key
            success: (res) => {

            },
          });
          //获取useid
          my.getStorage({
            key: 'userId',
            success: function(res) {
              that.setData({
                userId: res.data
              })
            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
          // 获取token
          my.getStorage({
            key: 'token',
            success: function(res) {
              that.setData({
                token: res.data
              })
            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
          that.post()
        } else {
          that.setData({
            outline: true
          })
        }
      }
    })
  },

  //获取页面信息
  post: function() {
    var that = this;
    that.setData({
      hidden: true
    })
    var req = {
      userid: that.data.userId,
      token: that.data.token
    }
    my.request({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      url: 'https://www.tiantong369.com/crm/usershopaddresslist', // 该url是自己的服务地址
      method: 'POST',
      headers: {
        // 'x-access-token': that.data.token
      },
      data: req,
      dataType: 'json',
      success: (res) => {// 成功获取订单号
        if (res.data.status == 200) {
          that.setData({
            hidden: false
          })
          var arr = [];
          for (var i = 0; i < res.data.data.msg.length; i++) {
            arr.push(res.data.data.msg[i])
            if (res.data.data.msg[i].isdefault == '是') {
              my.setStorage({
                key: 'addressId', // 缓存数据的key
                data: res.data.data.msg[i].id, // 要缓存的数据
                success: (res) => {

                },
              });
            }
          }
          that.setData({
            list: arr,
            noData: false
          })
          if (that.data.list.length == 0) {
            that.setData({
              noData: true
            })
          }
        } else {
          my.showToast({
            success: (res) => {
              content: res.data.message
            }
          });
        }


      },
      fail: (res) => {
        console.log('GetEmpPrice:', res)
      },
    });
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  bindKeyInput(e) {
    this.setData({
      code: e.detail.value
    });
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
});
